import os, os.path
import re
import sys
import glob
import platform


def BuildLibPAWN(env):
    pawnVersion = '3.3.4127'
    pawn_Dir = '../../libs/pawn-'+pawnVersion+'/source/'
    PAWN_SOURCES  = glob.glob(pawn_Dir+'amx/*.c')
    PAWN_SOURCES += glob.glob(pawn_Dir+'amx/*.cpp')
    print PAWN_SOURCES
    env_pawn = env.Clone();
    PAWN_SOURCES_Platform = []
    if env['OS'] in ("Darwin", "Linux"):
		# Remove a few entries that are not required.
		PAWN_SOURCES.remove(pawn_Dir+'amx/amxprocess.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx/dllmain.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx/pawnrun.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx/term_ga.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx/termwin.c')	
		PAWN_SOURCES_Platform += glob.glob(pawn_Dir+'/linux/*.c')
		PAWN_SOURCES_Platform += glob.glob(pawn_Dir+'/linux/*.cpp')
		#TODO:  fix this on other platforms.
		env_pawn.Append(CPPDEFINES = ['PAWN_CELL_SIZE=64','HAVE_I64'])
		env_pawn.Append(LIBS = ['m','dl'])
    elif env['OS'] in ("Windows"):
		# Remove a few entries that are not required.
		PAWN_SOURCES.remove(pawn_Dir+'amx\\amxprocess.c')
		#PAWN_SOURCES.remove(pawn_Dir+'amx\\dllmain.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx\\pawnrun.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx\\term_ga.c')
		PAWN_SOURCES.remove(pawn_Dir+'amx\\termwin.c')	
		env_pawn.Append(CPPDEFINES = ['PAWN_CELL_SIZE=64'])		
        #TODO:  fix this on other platforms.
		#env.Append(LIBS = ['m','dl'])	
    print "Building libPAWN"
    #it might be better to add this define for compile... AMX_NODYNALOAD
    env_pawn.Append(CPPPATH=[pawn_Dir+'linux',pawn_Dir+'compiler'])
    env_pawn.Append(CPPDEFINES = ['NO_MAIN'])
    # build the Pawn environment.
    pawn_lib = env_pawn.StaticLibrary(
        target = 'out/libs/libPAWN',
        source = [PAWN_SOURCES,PAWN_SOURCES_Platform])
    env_pawn.Program(target = 'out/bin/humble_pawnrun', source =
        [pawn_Dir+'/amx/pawnrun.c',pawn_lib])

    # Build the Compiler

    PAWN_SOURCES_CC  = glob.glob(pawn_Dir+'compiler/*.c')
    PAWN_SOURCES_CC += glob.glob(pawn_Dir+'compiler/*.cpp')
    if env['OS'] in ("Darwin", "Linux"):	
		#remove Entries from the found files
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler/scstub.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler/pawndisasm.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler/scpack.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler/pawncc.c')
    elif env['OS'] in ("Windows"):
		#remove Entries from the found files
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler\\scstub.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler\\pawndisasm.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler\\scpack.c')
		PAWN_SOURCES_CC.remove( pawn_Dir+'compiler\\pawncc.c')	
    env_pawn.Program(target = 'out/bin/humble_pawncc', source =
        [pawn_Dir+'/compiler/pawncc.c',PAWN_SOURCES_CC,PAWN_SOURCES_Platform])
    env_pawn.Program(target = 'out/bin/humble_pawndisasm', source =
        [pawn_Dir+'/compiler/pawndisasm.c',PAWN_SOURCES_CC,PAWN_SOURCES_Platform])
    env_pawn.Program(target = 'out/bin/humble_pawn_scpack', source =
        [pawn_Dir+'/compiler/scpack.c',PAWN_SOURCES_CC,PAWN_SOURCES_Platform])

    return pawn_lib
